package framework;

import interfaces.Board;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

/** 
 * A simple version of the visualizer that can be instantiated in the plugin
 */
@SuppressWarnings("serial")
public class VisualizerImpl extends VisualizerAbstract implements ActionListener{
	private Framework fw;
	
	/**
	 * Calls the constructor of the VisualizerAbstract class
	 * @param board - the board that the visualizer uses
	 */
	public VisualizerImpl(Board board, Framework fw){
		super(board);
		this.fw = fw;
		JFrame frm = new JFrame();
		JButton stop = new JButton("stop");
		JButton play = new JButton("play");
		JButton step = new JButton("step");
		JButton rewind = new JButton("rewind");
		JPanel pnl = new JPanel();
		frm.setVisible(true);
		frm.setSize(300,300);
		frm.setLocationRelativeTo(null);
		frm.add(pnl);
		pnl.add(stop);
		pnl.add(play);
		pnl.add(step);
		pnl.add(rewind);
		stop.addActionListener(this);
		play.addActionListener(this);
		step.addActionListener(this);
		rewind.addActionListener(this);	
         }

	@Override
	public void actionPerformed(ActionEvent arg0) {
		if(arg0.getActionCommand().equalsIgnoreCase("stop"))
			try {
				fw.stop();
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
		if(arg0.getActionCommand().equalsIgnoreCase("play"))
			try {
				fw.play();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		if(arg0.getActionCommand().equalsIgnoreCase("step"))
			try {
				fw.one_tick();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		if(arg0.getActionCommand().equalsIgnoreCase("rewind"))
			fw.rewind();
			
	}
}
